﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace LOPA
{
    public partial class LopaWorkPage : UserControl
    {
        public LopaWorkPage(LOPAWorkNode workNode)
        {
            InitializeComponent();
            LoadData(workNode);
            InitialDataSoure();
            Program.Workspace.PidListChanged += new Action<Dictionary<string, string>>(Workspace_PidListChanged);
            Program.Workspace.SafeListChanged += new Action<List<string>>(Workspace_SafeListChanged);
            Program.Workspace.EnvListChanged += new Action<List<string>>(Workspace_EnvListChanged);
            Program.Workspace.BusinessListChanged += new Action<List<string>>(Workspace_BusinessListChanged);
            this.tableLayoutPanel1.MouseWheel += new MouseEventHandler(LopaWorkPage_MouseWheel);
            this.tableLayoutPanel1.MouseDown += new MouseEventHandler(tableLayoutPanel1_MouseDown);
            this.MouseWheel +=new MouseEventHandler(LopaWorkPage_MouseWheel);
        }

        void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e){
            tableLayoutPanel1.Focus();
        }

        void LopaWorkPage_MouseWheel(object sender, MouseEventArgs e)
        {
            Point mousePoint = new Point(e.X, e.Y);
            mousePoint.Offset(this.Location.X, this.Location.Y);
            if (tableLayoutPanel1.RectangleToScreen(tableLayoutPanel1.DisplayRectangle).Contains(mousePoint))
            {
                tableLayoutPanel1.AutoScrollPosition = new Point(0, tableLayoutPanel1.VerticalScroll.Value - e.Delta);
            }
        }

        void Workspace_BusinessListChanged(List<string> obj){
            cbxBusiness.DataSource = obj;
        }

        void Workspace_EnvListChanged(List<string> obj){
            cbxEnv.DataSource = obj;
        }

        private void Workspace_SafeListChanged(List<string> obj){
            cbxSafe.DataSource = obj;
        }

        private void InitialDataSoure()
        {
            List<string> safeList = new List<string>();
            List<string> envList = new List<string>();
            List<string> businessList = new List<string>();
            Dictionary<string, double>.KeyCollection safeKeys = Program.Workspace.GetSafeFreList().Keys;
            Dictionary<string, double>.KeyCollection envKeys = Program.Workspace.GetEnvFreList().Keys;
            Dictionary<string, double>.KeyCollection businessKeys = Program.Workspace.GetBusinessFreList().Keys;
            foreach (string s in safeKeys){
                safeList.Add(s);
            }
            foreach (string s in envKeys)
            {
                envList.Add(s);
            }
            foreach (string s in businessKeys)
            {
                businessList.Add(s);
            }
            cbxSafe.DataSource = safeList;
            cbxEnv.DataSource = envList;
            cbxBusiness.DataSource = businessList;
        }

        void Workspace_PidListChanged(Dictionary<string, string> dictionary)
        {
            if (lblPids == null || lblPids.Text.Equals(""))
            {
                return;
            }
            List<string> newPids = new List<string>();
            string[] oldPids = lblPids.Text.Split(new Char[] { ',' });
            foreach (string s in oldPids)
            {
                if (dictionary.ContainsKey(s))
                {
                    newPids.Add(dictionary[s]);
                }
                else
                {
                    newPids.Add(s);
                }
            }
            string newText = string.Empty;
            foreach (string pid in newPids)
            {
                newText += pid + ",";
            }
            newText = newText.Substring(0, newText.Length - 1);
            lblPids.Text = newText;
            SaveWithoutRemider();
        }

        public LopaWorkPage()
        {
            InitializeComponent();
        }

        private void LoadData(LOPAWorkNode node){
            if (node.ProjectName != null){
                lblProject.Text = node.ProjectName;
            }
            if (node.EventName != null)
            {
                lblEventName.Text = node.EventName;
            }
            if (node.EventNum != null)
            {
                lblEventNum.Text = node.EventNum;
            }
            if (node.AssessDate != null)
            {
                lblEventDate.Text = node.AssessDate;
            }
            if (node.PidList != null)
            {
                lblPids.Text = node.PidList;
            }
            if (node.RelatedDevices != null)
            {
                tbxRelateProjects.Text = node.RelatedDevices;
            }
            if (node.RelatedProjects != null)
            {
                tbxRelateProjects.Text = node.RelatedProjects;
            }
            if (node.EventDesc != null)
            {
                tbxEventDesc.Text = node.EventDesc;
            }
            if (node.RiskCriteriaSafe != null)
            {
                cbxSafe.Text = node.RiskCriteriaSafe;
            }
            if (node.RiskCriteriaSafeDesc != null)
            {
                tbxSafeDesc.Text = node.RiskCriteriaSafeDesc;
            }
            if (node.RiskCriteriaSafeFrequence != null)
            {
                lblFrequenceS.Text = node.RiskCriteriaSafeFrequence;
            }
            if (node.RiskCriteriaEnv != null)
            {
                cbxEnv.Text = node.RiskCriteriaEnv;
            }
            if (node.RiskCriteriaEnvDesc != null)
            {
                tbxEnvDesc.Text = node.RiskCriteriaEnvDesc;
            }
            if (node.RiskCriteriaEnvFrequence != null)
            {
                lblFrequenceE.Text = node.RiskCriteriaEnvFrequence;
            }
            if (node.RiskcriteriaBusi != null)
            {
                cbxBusiness.Text = node.RiskcriteriaBusi;
            }
            if (node.RiskCriteriaBusiDesc != null)
            {
                tbxBusinessDesc.Text = node.RiskCriteriaBusiDesc;
            }
            if (node.RiskCriteriaBusiFrequence != null)
            {
                lblFrequenceB.Text = node.RiskCriteriaBusiFrequence;
            }
            if (node.CauseEventDesc != null)
            {
                tbxCauseEventDesc.Text = node.CauseEventDesc;
            }
            if (node.CauseEventFrequence != null)
            {
                tbxCauseEventFrequence.Text = node.CauseEventFrequence;
            }
            if (node.ConditionFactorFrequence1 != null)
            {
                tbxTimeFre.Text = node.ConditionFactorFrequence1;
            }
            if (node.ConditionFactorFrequence2 != null)
            {
                cbxCondition2.Text = node.ConditionFactorFrequence2;
            }
            if (node.ConditionFactorFrequence3 != null)
            {
                cbxCondition3.Text = node.ConditionFactorFrequence3;
            }
            if (node.ConditionFactorFrequence4 != null)
            {
                cbxCondition4.Text = node.ConditionFactorFrequence4;
            }
            if (node.ConditionFactorFrequence5 != null)
            {
                tbxOtherFre.Text = node.ConditionFactorFrequence5;
            }
            if (node.ConditionFactorEventFrequence != null)
            {
                lblEventModifyFrequence.Text = node.ConditionFactorEventFrequence;
            }
            if (node.CurrDepLevelType1 != null)
            {
                cbxCurrentDepLevel1.Text = node.CurrDepLevelType1;
            }
            if (node.CurrDepLevelType2 != null)
            {
                cbxCurrentDepLevel2.Text = node.CurrDepLevelType2;
            }
            if (node.CurrDepLevelType3 != null)
            {
                cbxCurrentDepLevel3.Text = node.CurrDepLevelType3;
            }
            if (node.CurrDepLevelType4 != null)
            {
                cbxCurrentDepLevel4.Text = node.CurrDepLevelType4;
            }
            if (node.CurrDepLevelType5 != null)
            {
                cbxCurrentDepLevel5.Text = node.CurrDepLevelType5;
            }
            if (node.CurrDepLevelType6 != null)
            {
                cbxCurrentDepLevel6.Text = node.CurrDepLevelType6;
            }
            if (node.CurrDepLevelDesc1 != null)
            {
                tbxCurrentDepLevelDesc1.Text = node.CurrDepLevelType1;
            }
            if (node.CurrDepLevelDesc2 != null)
            {
                tbxCurrentDepLevelDesc2.Text = node.CurrDepLevelType2;
            }
            if (node.CurrDepLevelDesc3 != null)
            {
                tbxCurrentDepLevelDesc3.Text = node.CurrDepLevelType3;
            }
            if (node.CurrDepLevelDesc4 != null)
            {
                tbxCurrentDepLevelDesc4.Text = node.CurrDepLevelType4;
            }
            if (node.CurrDepLevelDesc5 != null)
            {
                tbxCurrentDepLevelDesc5.Text = node.CurrDepLevelType5;
            }
            if (node.CurrDepLevelDesc6 != null)
            {
                tbxCurrentDepLevelDesc6.Text = node.CurrDepLevelType6;
            }
            if (node.CurrPfd1 != null)
            {
                tbxCurrentPfd1.Text = node.CurrPfd1;
            }
            if (node.CurrPfd2 != null)
            {
                tbxCurrentPfd2.Text = node.CurrPfd2;
            }
            if (node.CurrPfd3 != null)
            {
                tbxCurrentPfd3.Text = node.CurrPfd3;
            }
            if (node.CurrPfd4 != null)
            {
                tbxCurrentPfd4.Text = node.CurrPfd4;
            }
            if (node.CurrPfd5 != null)
            {
                tbxCurrentPfd5.Text = node.CurrPfd5;
            }
            if (node.CurrPfd6 != null)
            {
                tbxCurrentPfd6.Text = node.CurrPfd6;
            }
            if (node.CurrDepLevelPfd != null)
            {
                lblCurrentDepLevelPfd.Text = node.CurrDepLevelPfd;
            }
            if (node.AssessResSafeLevel != null)
            {
                lblDepLevelRes11.Text = node.AssessResSafeLevel;
            }
            if (node.AssessResSafeLopaConclusion != null)
            {
                lblDepLevelRes12.Text = node.AssessResSafeLopaConclusion;
            }
            if (node.AssessResSafeSil != null)
            {
                lblDepLevelRes13.Text = node.AssessResSafeSil;
            }
            if (node.AssessResEnvLevel != null)
            {
                lblDepLevelRes21.Text = node.AssessResEnvLevel;
            }
            if (node.AssessResEnvLopaConclusion != null)
            {
                lblDepLevelRes22.Text = node.AssessResEnvLopaConclusion;
            }
            if (node.AssessResEnvSil != null)
            {
                lblDepLevelRes23.Text = node.AssessResEnvSil;
            }
            if (node.AssessResBusiLevel != null)
            {
                lblDepLevelRes31.Text = node.AssessResBusiLevel;
            }
            if (node.AssessResBusiLopaConclusion != null)
            {
                lblDepLevelRes32.Text = node.AssessResBusiLopaConclusion;
            }
            if (node.AssessResBusiSil != null)
            {
                lblDepLevelRes33.Text = node.AssessResBusiSil;
            }
            if (node.AssessNewResSafeLevel != null)
            {
                lblNewDepLevelRes11.Text = node.AssessNewResSafeLevel;
            }
            if (node.AssessNewResSafeLopaConclusion != null)
            {
                lblNewDepLevelRes12.Text = node.AssessNewResSafeLopaConclusion;
            }
            if (node.AssessNewResSafeSil != null)
            {
                lblNewDepLevelRes13.Text = node.AssessNewResSafeSil;
            }
            if (node.AssessNewResEnvLevel != null)
            {
                lblNewDepLevelRes21.Text = node.AssessNewResEnvLevel;
            }
            if (node.AssessNewResEnvLopaConclusion != null)
            {
                lblNewDepLevelRes22.Text = node.AssessNewResEnvLopaConclusion;
            }
            if (node.AssessNewResEnvSil != null)
            {
                lblNewDepLevelRes23.Text = node.AssessNewResEnvSil;
            }
            if (node.AssessNewResBusiLevel != null)
            {
                lblNewDepLevelRes31.Text = node.AssessNewResBusiLevel;
            }
            if (node.AssessNewResBusiLopaConclusion != null)
            {
                lblNewDepLevelRes32.Text = node.AssessNewResBusiLopaConclusion;
            }
            if (node.AssessNewResBusiSil != null)
            {
                lblNewDepLevelRes33.Text = node.AssessNewResBusiSil;
            }
            if (node.NewDepLevelType1 != null)
            {
                cbxNewDepLevel1.Text = node.NewDepLevelType1;
            }
            if (node.NewDepLevelType2 != null)
            {
                cbxNewDepLevel2.Text = node.NewDepLevelType2;
            }
            if (node.NewDepLevelType3 != null)
            {
                cbxNewDepLevel3.Text = node.NewDepLevelType3;
            }
            if (node.NewDepLevelType4 != null)
            {
                cbxNewDepLevel4.Text = node.NewDepLevelType4;
            }
            if (node.NewDepLevelType5 != null)
            {
                cbxNewDepLevel5.Text = node.NewDepLevelType5;
            }
            if (node.NewDepLevelType6 != null)
            {
                cbxNewDepLevel6.Text = node.NewDepLevelType6;
            }
            if (node.NewDepLevelDesc1 != null)
            {
                tbxNewDepLevelDesc1.Text = node.NewDepLevelDesc1;
            }
            if (node.NewDepLevelDesc2 != null)
            {
                tbxNewDepLevelDesc2.Text = node.NewDepLevelDesc2;
            }
            if (node.NewDepLevelDesc3 != null)
            {
                tbxNewDepLevelDesc3.Text = node.NewDepLevelDesc3;
            }
            if (node.NewDepLevelDesc4 != null)
            {
                tbxNewDepLevelDesc4.Text = node.NewDepLevelDesc4;
            }
            if (node.NewDepLevelDesc5 != null)
            {
                tbxNewDepLevelDesc5.Text = node.NewDepLevelDesc5;
            }
            if (node.NewDepLevelDesc6 != null)
            {
                tbxNewDepLevelDesc6.Text = node.NewDepLevelDesc6;
            }
            if (node.NewPfd1 != null)
            {
                tbxNewPfd1.Text = node.NewPfd1;
            }
            if (node.NewPfd2 != null)
            {
                tbxNewPfd2.Text = node.NewPfd2;
            }
            if (node.NewPfd3 != null)
            {
                tbxNewPfd3.Text = node.NewPfd3;
            }
            if (node.NewPfd4 != null)
            {
                tbxNewPfd4.Text = node.NewPfd4;
            }
            if (node.NewPfd5 != null)
            {
                tbxNewPfd5.Text = node.NewPfd5;
            }
            if (node.NewPfd6 != null)
            {
                tbxNewPfd6.Text = node.NewPfd6;
            }
            if (node.NewDepLevelPfd != null)
            {
                lblNewDepLevelPfd.Text = node.NewDepLevelPfd;
            }
            if (node.Advice != null)
            {
                tbxAdvice.Text = node.Advice;
            }
            if (node.EventFrequenceUnderCurrLevel != null)
            {
                lblCurrentDepLevelEventFrequence.Text = node.EventFrequenceUnderCurrLevel;
            }
            if (node.EventFrequenceUnderNewLevel != null)
            {
                lblNewDepLevelEventFrequence.Text = node.EventFrequenceUnderNewLevel;
            }

        }

        public void SaveWithoutRemider(){

            LOPAWorkNode node = Program.Workspace.GetWorkNodeByNodeName(lblEventName.Text);
            node.ProjectName = lblProject.Text;
            node.EventName = lblEventName.Text;
            node.EventNum = lblEventNum.Text;
            node.AssessDate = lblEventDate.Text;
            node.PidList = lblPids.Text;
            node.RelatedDevices = tbxDevices.Text;
            node.RelatedProjects = tbxRelateProjects.Text;
            node.EventDesc = tbxEventDesc.Text;
            node.RiskCriteriaSafe = cbxSafe.Text;
            node.RiskCriteriaSafeDesc = tbxSafeDesc.Text;
            node.RiskCriteriaSafeFrequence = lblFrequenceS.Text;
            node.RiskCriteriaEnv = cbxEnv.Text;
            node.RiskCriteriaEnvDesc = tbxEnvDesc.Text;
            node.RiskCriteriaEnvFrequence = lblFrequenceE.Text;
            node.RiskcriteriaBusi = cbxBusiness.Text;
            node.RiskCriteriaBusiDesc = tbxBusinessDesc.Text;
            node.RiskCriteriaBusiFrequence = lblFrequenceB.Text;
            node.CauseEventDesc = tbxCauseEventDesc.Text;
            node.CauseEventFrequence = tbxCauseEventFrequence.Text;
            node.ConditionFactorFrequence1 = tbxTimeFre.Text;
            node.ConditionFactorFrequence2 = cbxCondition2.Text;
            node.ConditionFactorFrequence3 = cbxCondition3.Text;
            node.ConditionFactorFrequence4 = cbxCondition4.Text;
            node.ConditionFactorFrequence5 = tbxOtherFre.Text;
            node.ConditionFactorEventFrequence = lblEventModifyFrequence.Text;
            node.CurrDepLevelType1 = cbxCurrentDepLevel1.Text;
            node.CurrDepLevelType2 = cbxCurrentDepLevel2.Text;
            node.CurrDepLevelType3 = cbxCurrentDepLevel3.Text;
            node.CurrDepLevelType4 = cbxCurrentDepLevel4.Text;
            node.CurrDepLevelType5 = cbxCurrentDepLevel5.Text;
            node.CurrDepLevelType6 = cbxCurrentDepLevel6.Text;
            node.CurrDepLevelDesc1 = tbxCurrentDepLevelDesc1.Text;
            node.CurrDepLevelDesc2 = tbxCurrentDepLevelDesc2.Text;
            node.CurrDepLevelDesc3 = tbxCurrentDepLevelDesc3.Text;
            node.CurrDepLevelDesc4 = tbxCurrentDepLevelDesc4.Text;
            node.CurrDepLevelDesc5 = tbxCurrentDepLevelDesc5.Text;
            node.CurrPfd1 = tbxCurrentPfd1.Text;
            node.CurrPfd2 = tbxCurrentPfd2.Text;
            node.CurrPfd3 = tbxCurrentPfd3.Text;
            node.CurrPfd4 = tbxCurrentPfd4.Text;
            node.CurrPfd5 = tbxCurrentPfd5.Text;
            node.CurrDepLevelPfd = lblCurrentDepLevelPfd.Text;
            node.AssessResSafeLevel = lblDepLevelRes11.Text;
            node.AssessResSafeLopaConclusion = lblDepLevelRes12.Text;
            node.AssessResSafeSil = lblDepLevelRes13.Text;
            node.AssessResEnvLevel = lblDepLevelRes21.Text;
            node.AssessResEnvLopaConclusion = lblDepLevelRes22.Text;
            node.AssessResEnvSil = lblDepLevelRes23.Text;
            node.AssessResBusiLevel = lblDepLevelRes31.Text;
            node.AssessResBusiLopaConclusion = lblDepLevelRes32.Text;
            node.AssessResBusiSil = lblDepLevelRes33.Text;
            node.AssessNewResSafeLevel = lblNewDepLevelRes11.Text;
            node.AssessNewResSafeLopaConclusion = lblNewDepLevelRes12.Text;
            node.AssessNewResSafeSil = lblNewDepLevelRes13.Text;
            node.AssessNewResEnvLevel = lblNewDepLevelRes21.Text;
            node.AssessNewResEnvLopaConclusion = lblNewDepLevelRes22.Text;
            node.AssessNewResEnvSil = lblNewDepLevelRes23.Text;
            node.AssessNewResBusiLevel = lblNewDepLevelRes31.Text;
            node.AssessNewResBusiLopaConclusion = lblNewDepLevelRes32.Text;
            node.AssessNewResBusiSil = lblNewDepLevelRes33.Text;
            node.NewDepLevelType1 = cbxNewDepLevel1.Text;
            node.NewDepLevelType2 = cbxNewDepLevel2.Text;
            node.NewDepLevelType3 = cbxNewDepLevel3.Text;
            node.NewDepLevelType4 = cbxNewDepLevel4.Text;
            node.NewDepLevelType5 = cbxNewDepLevel5.Text;
            node.NewDepLevelType6 = cbxNewDepLevel6.Text;
            node.NewDepLevelDesc1 = tbxNewDepLevelDesc1.Text;
            node.NewDepLevelDesc2 = tbxNewDepLevelDesc2.Text;
            node.NewDepLevelDesc3 = tbxNewDepLevelDesc3.Text;
            node.NewDepLevelDesc4 = tbxNewDepLevelDesc4.Text;
            node.NewDepLevelDesc5 = tbxNewDepLevelDesc5.Text;
            node.NewDepLevelDesc6 = tbxNewDepLevelDesc6.Text;
            node.NewPfd1 = tbxNewPfd1.Text;
            node.NewPfd2 = tbxNewPfd2.Text;
            node.NewPfd3 = tbxNewPfd3.Text;
            node.NewPfd4 = tbxNewPfd4.Text;
            node.NewPfd5 = tbxNewPfd5.Text;
            node.NewPfd6 = tbxNewPfd6.Text;
            node.NewDepLevelPfd = lblNewDepLevelPfd.Text;
            node.Advice = tbxAdvice.Text;
            node.EventFrequenceUnderCurrLevel = lblCurrentDepLevelEventFrequence.Text;
            node.EventFrequenceUnderNewLevel = lblNewDepLevelEventFrequence.Text;
        }

        private void cbxSafe_SelectedIndexChanged(object sender, EventArgs e){
            double value = Program.Workspace.GetSafeFreList()[cbxSafe.Text];
            if (value == -1){
                lblFrequenceS.Text = "应定量分析";
                lblDepLevelRes11.Text = "应定量分析";
                lblDepLevelRes12.Text = "应定量分析";
                lblDepLevelRes13.Text = "应定量分析";
                if (!tbxNewPfd1.Text.Equals("")){
                    lblNewDepLevelRes11.Text = "应定量分析";
                    lblNewDepLevelRes12.Text = "应定量分析";
                    lblNewDepLevelRes13.Text = "应定量分析";
                }
            }
            else{
                lblFrequenceS.Text = value.ToString();
                if (lblCurrentDepLevelPfd.Text.Equals("")){
                    lblDepLevelRes11.Text = "";
                    lblDepLevelRes12.Text = "";
                    lblDepLevelRes13.Text = "";
                }
                if (lblNewDepLevelPfd.Text.Equals(""))
                {
                    lblNewDepLevelRes11.Text = "";
                    lblNewDepLevelRes12.Text = "";
                    lblNewDepLevelRes13.Text = "";
                }
            }
        }

        private void cbxEnv_SelectedIndexChanged(object sender, EventArgs e)
        {
            double value = Program.Workspace.GetEnvFreList()[cbxEnv.Text];
            if (value == -1){
                lblFrequenceE.Text = "应定量分析";
                lblDepLevelRes21.Text = "应定量分析";
                lblDepLevelRes22.Text = "应定量分析";
                lblDepLevelRes23.Text = "应定量分析";
                if (!tbxNewPfd1.Text.Equals(""))
                {
                    lblNewDepLevelRes21.Text = "应定量分析";
                    lblNewDepLevelRes22.Text = "应定量分析";
                    lblNewDepLevelRes23.Text = "应定量分析";  
                }
            }
            else{
                lblFrequenceE.Text = value.ToString();
                if (lblCurrentDepLevelPfd.Text.Equals("")){
                    lblDepLevelRes21.Text = "";
                    lblDepLevelRes22.Text = "";
                    lblDepLevelRes23.Text = "";
                }
                if(lblNewDepLevelPfd.Text.Equals("")){
                    lblNewDepLevelRes21.Text = "";
                    lblNewDepLevelRes22.Text = "";
                    lblNewDepLevelRes23.Text = "";
                }
            }
        }

        private void cbxBusiness_SelectedIndexChanged(object sender, EventArgs e)
        {
            double value = Program.Workspace.GetBusinessFreList()[cbxBusiness.Text];
            if (value == -1){
                lblFrequenceB.Text = "应定量分析";
                lblDepLevelRes31.Text = "应定量分析";
                lblDepLevelRes32.Text = "应定量分析";
                lblDepLevelRes33.Text = "应定量分析";
                if (!tbxNewPfd1.Text.Equals(""))
                {
                    lblNewDepLevelRes31.Text = "应定量分析";
                    lblNewDepLevelRes32.Text = "应定量分析";
                    lblNewDepLevelRes33.Text = "应定量分析";
                }
            }
            else{
                lblFrequenceB.Text = value.ToString();
                if (lblCurrentDepLevelPfd.Text.Equals("")){
                    lblDepLevelRes31.Text = "";
                    lblDepLevelRes32.Text = "";
                    lblDepLevelRes33.Text = "";
                }
                if (lblNewDepLevelPfd.Text.Equals(""))
                {
                    lblNewDepLevelRes31.Text = "";
                    lblNewDepLevelRes32.Text = "";
                    lblNewDepLevelRes33.Text = "";
                }
            }

        }

        private void tbxCauseEventFrequence_TextChanged(object sender, EventArgs e)
        {
            if (tbxTimeFre.Text.Equals("") || cbxCondition2.Text.Equals("") || cbxCondition3.Text.Equals("")
                || cbxCondition4.Text.Equals("") || tbxOtherFre.Text.Equals(""))
                return;
            else{
                double a, b, c, d, f, g;
                double.TryParse(tbxTimeFre.Text, out a);
                double.TryParse(cbxCondition2.Text, out b);
                double.TryParse(cbxCondition3.Text, out c);
                double.TryParse(cbxCondition4.Text, out d);
                double.TryParse(tbxOtherFre.Text, out f);
                double.TryParse(tbxCauseEventFrequence.Text, out g);
                lblEventModifyFrequence.Text = (a*b*c*d*g*f).ToString("e2");
            }

            
        }

        public TextBox TbxAdvice{
            get { return tbxAdvice; }
            set { tbxAdvice = value; }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e){
            tbxDevices.Enabled = true;
            tbxRelateProjects.Enabled = true;
            tbxEventDesc.Enabled = true;
            tbxSafeDesc.Enabled = true;
            tbxEnvDesc.Enabled = true;
            tbxBusinessDesc.Enabled = true;
            tbxCauseEventDesc.Enabled = true;
            tbxCauseEventFrequence.Enabled = true;
            tbxTimeFre.Enabled = true;
            tbxOtherFre.Enabled = true;
            tbxCurrentDepLevelDesc1.Enabled = true;
            tbxCurrentDepLevelDesc2.Enabled = true;
            tbxCurrentDepLevelDesc3.Enabled = true;
            tbxCurrentDepLevelDesc4.Enabled = true;
            tbxCurrentDepLevelDesc5.Enabled = true;
            tbxCurrentDepLevelDesc6.Enabled = true;
            tbxCurrentPfd1.Enabled = true;
            tbxCurrentPfd2.Enabled = true;
            tbxCurrentPfd3.Enabled = true;
            tbxCurrentPfd4.Enabled = true;
            tbxCurrentPfd5.Enabled = true;
            tbxCurrentPfd6.Enabled = true;
            tbxNewDepLevelDesc1.Enabled = true;
            tbxNewDepLevelDesc2.Enabled = true;
            tbxNewDepLevelDesc3.Enabled = true;
            tbxNewDepLevelDesc4.Enabled = true;
            tbxNewDepLevelDesc5.Enabled = true;
            tbxNewDepLevelDesc6.Enabled = true;
            tbxNewPfd1.Enabled = true;
            tbxNewPfd2.Enabled = true;
            tbxNewPfd3.Enabled = true;
            tbxNewPfd4.Enabled = true;
            tbxNewPfd5.Enabled = true;
            tbxNewPfd6.Enabled = true;
            tbxAdvice.Enabled = true;
            cbxSafe.Enabled = true;
            cbxEnv.Enabled = true;
            cbxBusiness.Enabled = true;
            cbxCondition2.Enabled = true;
            cbxCondition3.Enabled = true;
            cbxCondition4.Enabled = true;
            cbxCurrentDepLevel1.Enabled = true;
            cbxCurrentDepLevel2.Enabled = true;
            cbxCurrentDepLevel3.Enabled = true;
            cbxCurrentDepLevel4.Enabled = true;
            cbxCurrentDepLevel5.Enabled = true;
            cbxCurrentDepLevel6.Enabled = true;
            cbxNewDepLevel1.Enabled = true;
            cbxNewDepLevel2.Enabled = true;
            cbxNewDepLevel3.Enabled = true;
            cbxNewDepLevel4.Enabled = true;
            cbxNewDepLevel5.Enabled = true;
            cbxNewDepLevel6.Enabled = true;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e){
            SaveWithoutRemider();
            tbxDevices.Enabled = false;
            tbxRelateProjects.Enabled = false;
            tbxEventDesc.Enabled = false;
            tbxSafeDesc.Enabled = false;
            tbxEnvDesc.Enabled = false;
            tbxBusinessDesc.Enabled = false;
            tbxCauseEventDesc.Enabled = false;
            tbxCauseEventFrequence.Enabled = false;
            tbxTimeFre.Enabled = false;
            tbxOtherFre.Enabled = false;
            tbxCurrentDepLevelDesc1.Enabled = false;
            tbxCurrentDepLevelDesc2.Enabled = false;
            tbxCurrentDepLevelDesc3.Enabled = false;
            tbxCurrentDepLevelDesc4.Enabled = false;
            tbxCurrentDepLevelDesc5.Enabled = false;
            tbxCurrentDepLevelDesc6.Enabled = false;
            tbxCurrentPfd1.Enabled = false;
            tbxCurrentPfd2.Enabled = false;
            tbxCurrentPfd3.Enabled = false;
            tbxCurrentPfd4.Enabled = false;
            tbxCurrentPfd5.Enabled = false;
            tbxCurrentPfd6.Enabled = false;
            tbxNewDepLevelDesc1.Enabled = false;
            tbxNewDepLevelDesc2.Enabled = false;
            tbxNewDepLevelDesc3.Enabled = false;
            tbxNewDepLevelDesc4.Enabled = false;
            tbxNewDepLevelDesc5.Enabled = false;
            tbxNewDepLevelDesc6.Enabled = false;
            tbxNewPfd1.Enabled = false;
            tbxNewPfd2.Enabled = false;
            tbxNewPfd3.Enabled = false;
            tbxNewPfd4.Enabled = false;
            tbxNewPfd5.Enabled = false;
            tbxNewPfd6.Enabled = false;
            tbxAdvice.Enabled = false;
            cbxSafe.Enabled = false;
            cbxEnv.Enabled = false;
            cbxBusiness.Enabled = false;
            cbxCondition2.Enabled = false;
            cbxCondition3.Enabled = false;
            cbxCondition4.Enabled = false;
            cbxCurrentDepLevel1.Enabled = false;
            cbxCurrentDepLevel2.Enabled = false;
            cbxCurrentDepLevel3.Enabled = false;
            cbxCurrentDepLevel4.Enabled = false;
            cbxCurrentDepLevel5.Enabled = false;
            cbxCurrentDepLevel6.Enabled = false;
            cbxNewDepLevel1.Enabled = false;
            cbxNewDepLevel2.Enabled = false;
            cbxNewDepLevel3.Enabled = false;
            cbxNewDepLevel4.Enabled = false;
            cbxNewDepLevel5.Enabled = false;
            cbxNewDepLevel6.Enabled = false;
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
        }

        private void tbxChanged(object sender, EventArgs e){
            if (tbxCurrentPfd1.Text.Equals("") || tbxCurrentPfd2.Text.Equals("") || tbxCurrentPfd3.Text.Equals("")
                || tbxCurrentPfd4.Text.Equals("") || tbxCurrentPfd5.Text.Equals("") || tbxCurrentPfd6.Text.Equals(""))
                return;
            double a, b, c, d, f, g;
            double.TryParse(tbxCurrentPfd1.Text, out a);
            double.TryParse(tbxCurrentPfd2.Text, out b);
            double.TryParse(tbxCurrentPfd3.Text, out c);
            double.TryParse(tbxCurrentPfd4.Text, out d);
            double.TryParse(tbxCurrentPfd5.Text, out f);
            double.TryParse(tbxCurrentPfd6.Text, out g);
            lblCurrentDepLevelPfd.Text = (a * b * c * d * g * f).ToString("e2");
        }

        private void lblUVChanged(object sender, EventArgs e){
            double a, b, c;
            if(!lblEventModifyFrequence.Text.Equals("") && !lblCurrentDepLevelPfd.Text.Equals("")){
                double.TryParse(lblEventModifyFrequence.Text, out a);
                double.TryParse(lblCurrentDepLevelPfd.Text, out b);
                lblCurrentDepLevelEventFrequence.Text = (a*b).ToString("e2");
                if(!lblNewDepLevelPfd.Text.Equals("")){
                    double.TryParse(lblNewDepLevelPfd.Text, out c);
                    lblNewDepLevelEventFrequence.Text = (a*b*c).ToString("e2");
                }
            }
        }
        private void lblZChanged(object sender, EventArgs e){
            double a, b, c;
            if (!lblNewDepLevelPfd.Text.Equals("") && !lblEventModifyFrequence.Text.Equals("") && !lblCurrentDepLevelPfd.Text.Equals(""))
            {
                double.TryParse(lblEventModifyFrequence.Text, out a);
                double.TryParse(lblCurrentDepLevelPfd.Text, out b);
                double.TryParse(lblNewDepLevelPfd.Text, out c);
                lblNewDepLevelEventFrequence.Text = (a * b * c).ToString("e2");
            }
        }

        private void lblXChanged(object sender, EventArgs eventArgs){
            double s, e, b,x;
            List<string> res = new List<string>();
            if (lblCurrentDepLevelEventFrequence.Text.Equals(""))
                return;
            double.TryParse(lblCurrentDepLevelEventFrequence.Text, out x);
            if(!lblFrequenceS.Text.Equals("") && !lblFrequenceS.Text.Equals("应定量分析")){
                double.TryParse(lblFrequenceS.Text, out s);
                res = GetResult(s/x);
                if (res.Count != 2){
                    MessageBox.Show("计算错误");
                    return;
                }
                lblDepLevelRes11.Text = (s/x).ToString("e2");
                lblDepLevelRes12.Text = res[0];
                lblDepLevelRes13.Text = res[1];
            }
            if (!lblFrequenceE.Text.Equals("") && !lblFrequenceE.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceE.Text, out e);
                res = GetResult(e / x);
                if (res.Count != 2)
                {
                    MessageBox.Show("计算错误");
                    return;
                }
                lblDepLevelRes21.Text = (e / x).ToString("e2");
                lblDepLevelRes22.Text = res[0];
                lblDepLevelRes23.Text = res[1];
            }
            if (!lblFrequenceB.Text.Equals("") && !lblFrequenceB.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceB.Text, out b);
                res = GetResult(b / x);
                if (res.Count != 2)
                {
                    MessageBox.Show("计算错误");
                    return;
                }
                lblDepLevelRes31.Text = (b / x).ToString("e2");
                lblDepLevelRes32.Text = res[0];
                lblDepLevelRes33.Text = res[1];
            }
        }

        private void lblWChanged(object sender, EventArgs eventArgs ){
            double s, e, b, w;
            List<string> res = new List<string>();
            if (lblNewDepLevelEventFrequence.Text.Equals(""))
                return;
            double.TryParse(lblNewDepLevelEventFrequence.Text, out w);
            if (!lblFrequenceS.Text.Equals("") && !lblFrequenceS.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceS.Text, out s);
                res = GetResult(s / w);
                if (res.Count != 2)
                {
                    MessageBox.Show("计算错误");
                    return;
                }
                lblNewDepLevelRes11.Text = (s / w).ToString("e2");
                lblNewDepLevelRes12.Text = res[0];
                lblNewDepLevelRes13.Text = res[1];
            }
            if (!lblFrequenceE.Text.Equals("") && !lblFrequenceE.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceE.Text, out e);
                res = GetResult(e / w);
                if (res.Count != 2)
                {
                    MessageBox.Show("计算错误");
                    return;
                }
                lblNewDepLevelRes21.Text = (e / w).ToString("e2");
                lblNewDepLevelRes22.Text = res[0];
                lblNewDepLevelRes23.Text = res[1];
            }
            if (!lblFrequenceB.Text.Equals("") && !lblFrequenceB.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceB.Text, out b);
                res = GetResult(b / w);
                if (res.Count != 2)
                {
                    MessageBox.Show("计算错误");
                    return;
                }
                lblNewDepLevelRes31.Text = (b / w).ToString("e2");
                lblNewDepLevelRes32.Text = res[0];
                lblNewDepLevelRes33.Text = res[1];
            }
        }

        private void lblSChanged(object sender,EventArgs eventArgs){
            double s,x, w;
            if (lblCurrentDepLevelEventFrequence.Text.Equals("") && lblNewDepLevelEventFrequence.Text.Equals(""))
                return;
            if (!lblCurrentDepLevelEventFrequence.Text.Equals(""))
            {
                List<string> res = new List<string>();
                double.TryParse(lblCurrentDepLevelEventFrequence.Text, out x);
                if (!lblFrequenceS.Text.Equals("") && !lblFrequenceS.Text.Equals("应定量分析"))
                {
                    double.TryParse(lblFrequenceS.Text, out s);
                    res = GetResult(s / x);
                    if (res.Count != 2)
                    {
                        MessageBox.Show("计算错误");
                        return;
                    }
                    lblDepLevelRes11.Text = (s/x).ToString("e2");
                    lblDepLevelRes12.Text = res[0];
                    lblDepLevelRes13.Text = res[1];
                }
            }
            if (!lblNewDepLevelEventFrequence.Text.Equals(""))
            {
                List<string> res = new List<string>();
                double.TryParse(lblNewDepLevelEventFrequence.Text, out w);
                if (!lblFrequenceS.Text.Equals("") && !lblFrequenceS.Text.Equals("应定量分析"))
                {
                    double.TryParse(lblFrequenceS.Text, out s);
                    res = GetResult(s / w);
                    if (res.Count != 2)
                    {
                        MessageBox.Show("计算错误");
                        return;
                    }
                    lblNewDepLevelRes11.Text = (s/w).ToString("e2");
                    lblNewDepLevelRes12.Text = res[0];
                    lblNewDepLevelRes13.Text = res[1];
                }
            }
        }

        private void lblEChanged(object sender, EventArgs eventArgs){
            double e, x, w;
            if (lblCurrentDepLevelEventFrequence.Text.Equals("") && lblNewDepLevelEventFrequence.Text.Equals(""))
                return;
            if (!lblCurrentDepLevelEventFrequence.Text.Equals(""))
            {
                List<string> res = new List<string>();
                double.TryParse(lblCurrentDepLevelEventFrequence.Text, out x);
                if (!lblFrequenceE.Text.Equals("") && !lblFrequenceE.Text.Equals("应定量分析"))
                {
                    double.TryParse(lblFrequenceE.Text, out e);
                    res = GetResult(e / x);
                    if (res.Count != 2)
                    {
                        MessageBox.Show("计算错误");
                        return;
                    }
                    lblDepLevelRes21.Text = (e / x).ToString("e2");
                    lblDepLevelRes22.Text = res[0];
                    lblDepLevelRes23.Text = res[1];
                }
            }
            if (!lblNewDepLevelEventFrequence.Text.Equals(""))
            {
                List<string> res = new List<string>();
                double.TryParse(lblNewDepLevelEventFrequence.Text, out w);
                if (!lblFrequenceE.Text.Equals("") && !lblFrequenceE.Text.Equals("应定量分析"))
                {
                    double.TryParse(lblFrequenceE.Text, out e);
                    res = GetResult(e / w);
                    if (res.Count != 2)
                    {
                        MessageBox.Show("计算错误");
                        return;
                    }
                    lblNewDepLevelRes21.Text = (e / w).ToString("e2");
                    lblNewDepLevelRes22.Text = res[0];
                    lblNewDepLevelRes23.Text = res[1];
                }
            }
        }

        private void lblBChanged(object sender, EventArgs eventArgs){
            double b, x, w;
            if (lblCurrentDepLevelEventFrequence.Text.Equals("") && lblNewDepLevelEventFrequence.Text.Equals(""))
                return;
            if (!lblCurrentDepLevelEventFrequence.Text.Equals(""))
            {
                List<string> res = new List<string>();
                double.TryParse(lblCurrentDepLevelEventFrequence.Text, out x);
                if (!lblFrequenceB.Text.Equals("") && !lblFrequenceB.Text.Equals("应定量分析"))
                {
                    double.TryParse(lblFrequenceB.Text, out b);
                    res = GetResult(b / x);
                    if (res.Count != 2)
                    {
                        MessageBox.Show("计算错误");
                        return;
                    }
                    lblDepLevelRes31.Text = (b / x).ToString("e2");
                    lblDepLevelRes32.Text = res[0];
                    lblDepLevelRes33.Text = res[1];
                }
            }
            if (!lblNewDepLevelEventFrequence.Text.Equals(""))
            {
                List<string> res = new List<string>();
                double.TryParse(lblNewDepLevelEventFrequence.Text, out w);
                if (!lblFrequenceB.Text.Equals("") && !lblFrequenceB.Text.Equals("应定量分析"))
                {
                    double.TryParse(lblFrequenceB.Text, out b);
                    res = GetResult(b / w);
                    if (res.Count != 2)
                    {
                        MessageBox.Show("计算错误");
                        return;
                    }
                    lblNewDepLevelRes31.Text = (b / w).ToString("e2");
                    lblNewDepLevelRes32.Text = res[0];
                    lblNewDepLevelRes33.Text = res[1];
                }
            }
        }

        private List<string> GetResult(double x){
            List<string> res = new List<string>();
            if(x >= 1){
                res.Add("Pass");
                res.Add("Not Required");
            }else{
                res.Add("Fail");
                if(x>=0.1 && x < 1)
                    res.Add("No Special Integrity Reqmts");
                else if(x>=0.01 && x < 0.1){
                    res.Add("SIL-1");
                }else if(x>=0.001 && x < 0.01){
                    res.Add("SIL-2");
                }
                else{
                    res.Add("SIL-3");
                }
            }
            return res;
        }


        private void tbxChanged2(object sender, EventArgs e){
            if (tbxNewPfd1.Text.Equals("") || tbxNewPfd2.Text.Equals("") || tbxNewPfd3.Text.Equals("") ||
                tbxNewPfd4.Text.Equals("") || tbxNewPfd5.Text.Equals("") || tbxNewPfd6.Text.Equals(""))
                return;
            double a, b, c, d, f, g;
            double.TryParse(tbxNewPfd1.Text, out a);
            double.TryParse(tbxNewPfd2.Text, out b);
            double.TryParse(tbxNewPfd3.Text, out c);
            double.TryParse(tbxNewPfd4.Text, out d);
            double.TryParse(tbxNewPfd5.Text, out f);
            double.TryParse(tbxNewPfd6.Text, out g);
            lblNewDepLevelPfd.Text = (a*b*c*d*g*f).ToString("e2");
        }

        private void lblChanged2(object sender, EventArgs e){
            if (lblNewDepLevelPfd.Text.Equals(""))
                return;
            double d;
            double.TryParse(lblNewDepLevelPfd.Text, out d);
            if (d == 0)
                return;
            double a, b, c;
            if (!lblFrequenceS.Text.Equals("") && !lblFrequenceS.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceS.Text, out a);
                double x = a / d;
                lblDepLevelRes11.Text = (x).ToString("e2");
                if (x >= 1)
                {
                    lblDepLevelRes12.Text = "Pass";
                    lblDepLevelRes13.Text = "Not Required";
                }
                else
                {
                    lblDepLevelRes12.Text = "Fail";
                    if (x >= 0.1 && x < 1)
                    {
                        lblDepLevelRes13.Text = "No Special Integrity Reqmts";
                    }
                    else if (x >= 0.01 && x < 0.1)
                    {
                        lblDepLevelRes13.Text = "SIL-1";
                    }
                    else if (x >= 0.001 && x < 0.01)
                    {
                        lblDepLevelRes13.Text = "SIL-2";
                    }
                    else
                    {
                        lblDepLevelRes13.Text = "SIL-3";
                    }
                }
            }
            if (!lblFrequenceE.Text.Equals("") && !lblFrequenceE.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceE.Text, out b);
                double x = b / d;
                lblDepLevelRes21.Text = (b / d).ToString("e2");
                if (x >= 1)
                {
                    lblDepLevelRes22.Text = "Pass";
                    lblDepLevelRes23.Text = "Not Required";
                }
                else
                {
                    lblDepLevelRes22.Text = "Fail";
                    if (x >= 0.1 && x < 1)
                    {
                        lblDepLevelRes23.Text = "No Special Integrity Reqmts";
                    }
                    else if (x >= 0.01 && x < 0.1)
                    {
                        lblDepLevelRes23.Text = "SIL-1";
                    }
                    else if (x >= 0.001 && x < 0.01)
                    {
                        lblDepLevelRes23.Text = "SIL-2";
                    }
                    else
                    {
                        lblDepLevelRes23.Text = "SIL-3";
                    }
                }
            }
            if (!lblFrequenceB.Text.Equals("") && !lblFrequenceB.Text.Equals("应定量分析"))
            {
                double.TryParse(lblFrequenceB.Text, out c);
                double x = c / d;
                lblDepLevelRes31.Text = (c / d).ToString("e2");
                if (x >= 1)
                {
                    lblDepLevelRes32.Text = "Pass";
                    lblDepLevelRes33.Text = "Not Required";
                }
                else
                {
                    lblDepLevelRes32.Text = "Fail";
                    if (x >= 0.1 && x < 1)
                    {
                        lblDepLevelRes33.Text = "No Special Integrity Reqmts";
                    }
                    else if (x >= 0.01 && x < 0.1)
                    {
                        lblDepLevelRes33.Text = "SIL-1";
                    }
                    else if (x >= 0.001 && x < 0.01)
                    {
                        lblDepLevelRes33.Text = "SIL-2";
                    }
                    else
                    {
                        lblDepLevelRes33.Text = "SIL-3";
                    }
                }
            }
        }
    }
}
